//
//  ThemeMoreViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/28.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "ThemeMoreViewController.h"
#import "ProThemeViewController.h"  ///主题详情页
#import "ThemeModel.h"              ///本类数据的model类
#import "ThemeMoreCell.h"           ///自定义model类



@interface ThemeMoreViewController ()<UITableViewDataSource, UITableViewDelegate, PullTableViewDelegate>
/// 数据数组
@property (nonatomic, retain)NSMutableArray *dataArray;
/// 刷新加载表视图
@property (nonatomic, retain)CKTableView *tableView;

@end

@implementation ThemeMoreViewController


- (void)netWorking
{
    NSString *strUrl = @"http://hunliji.com/v1/api/app/newsubjects.json?page=1";
    [NetHandler networkWithURL:strUrl completion:^(id result) {
        NSArray *array = [result objectForKey:@"subjects"];
        for (NSDictionary *dic in array) {
            ThemeModel *model =[[ThemeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 二级页面隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 重写返回上级页面方法
    UIImage *backImage = [[UIImage imageNamed:@"返回.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popBack:)];
    
    self.dataArray = [[NSMutableArray alloc] init];
    [self createTableView];
    [self netWorking];
}
/// 返回上级页面
- (void)popBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createTableView
{
    self.tableView = [[CKTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 69) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.pullDelegate = self;
    [self.view addSubview:self.tableView];
}
#pragma mark 加载刷新
- (void) refreshTable
{
    [self.tableView reloadData];
    
    self.tableView.pullLastRefreshDate = [NSDate date];
    self.tableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
    //    //  加载更多实现代码放在在这
    [self shuaxinNetWork];
    self.tableView.pullTableIsLoadingMore = NO;
}
/// 加载更多
///  刷新方法
static NSInteger count = 2;
- (void)shuaxinNetWork
{
    NSString *strUrl = [NSString stringWithFormat:@"http://hunliji.com/v1/api/app/newsubjects.json?page=%ld", count];
    [NetHandler networkWithURL:strUrl completion:^(id result) {
        NSArray *array = [result objectForKey:@"subjects"];
        for (NSDictionary *dic in array) {
            ThemeModel *model =[[ThemeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    count ++;
}
#pragma mark -
#pragma mark - PullTableViewDelegate协议

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}

#pragma mark -

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count / 2;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"themeMore";
    ThemeMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[ThemeMoreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    ThemeModel *model1 = [self.dataArray objectAtIndex:indexPath.row * 2];
    ThemeModel *model2 = [self.dataArray objectAtIndex:indexPath.row * 2 + 1];
    cell.model1 = model1;
    cell.model2 = model2;
    cell.block = ^(ThemeModel *model){
        ProThemeViewController *theme = [[ProThemeViewController alloc] init];
        theme.model = model;
        [self.navigationController pushViewController:theme animated:YES];
    };
    return cell;
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (self.view.frame.size.width - 15) / 2;
    CGFloat w1 = 462.f;
    CGFloat h1 = 642.f;
    CGFloat h = h1 * (w / w1);
    return h;
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
